#include <func.h>

typedef struct node_s {
    char data[1024];
    struct node_s *pNext;
} node_t;

typedef struct taskStack_s {
    node_t *pFront;
    int size;
} taskStack_t;

int initStack(taskStack_t *taskStack, char *path);

int enStack(taskStack_t *taskStack, char *path);

int deStack(taskStack_t *taskStack);

int visitStack(taskStack_t *taskStack, char *path);

int visitStack(taskStack_t *taskStack, char *path) {
    char buf[1024];
    node_t *pCur = taskStack->pFront;
    
    strcpy(path, ""); // 初始化path为空字符串
    
    while (pCur) {
        strcpy(buf, pCur->data);
        strcat(buf, "/");
        strcat(buf, path);
        strcpy(path, buf);
        pCur = pCur->pNext;
    }
    
    // 去除末尾多余的斜杠
    if (strlen(path) > 0 && path[strlen(path) - 1] == '/') {
        path[strlen(path) - 1] = '\0';
    }
    
    return 0;
}

int visitStack1(taskStack_t *taskStack, char *path) {
    char buf[1024];
    node_t *pCur = taskStack->pFront;
    
    strcpy(path, ""); // 初始化path为空字符串
    
    while (pCur->pNext) {
        strcpy(buf, "/");
        strcat(buf, pCur->data);
        strcat(buf, path);
        strcpy(path, buf);
        pCur = pCur->pNext;
    }
    
    // 去除末尾多余的斜杠
    if (strlen(path) > 0 && path[strlen(path) - 1] == '/') {
        path[strlen(path) - 1] = '\0';
    }
    
    return 0;
}

int mycd(taskStack_t *pStack, char *arg, char *ret) {
    //拆分arg
    char *sockPath = strtok(arg, "/");
    while(1) {
        if(!sockPath) {
            break;
        }
        if(strcmp(sockPath, ".") != 0 && strcmp(sockPath, "..") != 0) {
            enStack(pStack, sockPath);
        } 
        if(strcmp(sockPath, "..") == 0) {
            deStack(pStack);
        }
        sockPath = strtok(NULL, "/");
    }
    return 0;
}

int main() {
    taskStack_t taskStact;
    char *path = "/home/test/zs";
    initStack(&taskStact, path);
    enStack(&taskStact, "666");
    enStack(&taskStact, "777");
    char ret[1024] = {0};
    /* char arg[] = "../123/../123"; */
    /* char arg[] = "A/abc/../A"; */
    /* char arg[] = "./A/abc/../A"; */
    /* mycd(&taskStact, arg, ret); */
    /* printf("data = %s, size = %d\n", taskStact.pFront->data, taskStact.size); */

    char buf[1024] = {0};
    visitStack1(&taskStact, buf);
    printf("buf = %s\n", buf);
    visitStack(&taskStact, buf);
    printf("buf = %s\n", buf);
    //enStack(&taskStact, "123");
    //printf("data = %s, size = %d\n", taskStact.pFront->data, taskStact.size);

    //enStack(&taskStact, "abc");
    //printf("data = %s, size = %d\n", taskStact.pFront->data, taskStact.size);

    /* deStack(&taskStact); */
    /* printf("data = %s, size = %d\n", taskStact.pFront->data, taskStact.size); */

    /* char buf[1024]; */
    /* visitStack1(&taskStact, buf); */
    /* printf("buf = %s\n", buf); */

    /* char buf1[1024]; */
    /* pwdExec(buf, buf1); */
    /* printf("buf = %s\n", buf); */
    /* printf("buf1 = %s\n", buf1); */
}

int initStack(taskStack_t *taskStack, char *path) {
    bzero(taskStack, sizeof(taskStack_t));
    node_t *pNew = (node_t *)calloc(1, sizeof(node_t));
    memcpy(pNew->data, path, strlen(path));
    pNew->pNext = NULL;
    taskStack->pFront = pNew;
    ++taskStack->size;
    return 0;
}

int enStack(taskStack_t *taskStack, char *path) {
    node_t *pNew = (node_t *)calloc(1, sizeof(node_t));
    memcpy(&pNew->data, path, sizeof(path));
    pNew->pNext = taskStack->pFront;
    taskStack->pFront = pNew;
    ++taskStack->size;
    return 0;
}

int deStack(taskStack_t *taskStack) {
    node_t *pCur = taskStack->pFront;
    if(taskStack->size > 1) {
        taskStack->pFront = pCur->pNext;
        free(pCur);
        --taskStack->size;
    }
    return 0;
}
